<template>
	<view>
		<u-form :rules="rules">
		<u-form-item label="简介">
			<u-input v-model="summary" />
		</u-form-item>
		
		<u-form-item label="标题" >
			<u-input v-model="title" />
		</u-form-item>
		
		<view>
			<u--textarea v-model="content" placeholder="请输入发布类容" focus="true" maxlength=-1 height=140></u--textarea>
		</view>
		
		<u-upload
				:fileList="fileList1"
				@afterRead="afterRead"
				@delete="deletePic"
				name="1"
				multiple=true
				:maxCount="10"
			></u-upload>
		
		<!-- <u-form-item label="内容" >
			<u-input v-model="content" />
		</u-form-item> -->
		<u-scroll-list class="cate-xz" :indicator="false">
			<u-radio-group v-model="category">
				<u-radio 
					v-for="(item, index) in categorysList" :key="index" 
					:name="item"
					:disabled="false"
					:label="item.categoryName"
				>
				</u-radio>
			</u-radio-group>
		</u-scroll-list>
		
		<u-scroll-list class=""  :indicator="false">
			<u-checkbox-group v-model="tags1">
				<view v-for="(item, index) in tagsList" :key="index" >
					<u-checkbox
						:name="item.id"
						:label="item.tagName"
					>
					</u-checkbox>
				</view>
			</u-checkbox-group>
	    </u-scroll-list>
		
		<button @click="open(content,title,category,summary,tags1,contentHtml)" type="primary">发布</button>
	    </u-form>
	</view>
</template>

<script>
export default {
	data() {
		return {
			content:'',
			// id:'',
			contentHtml:null,
			category:{},
			summary:'',
			tags1:[],
			title:'',
			tagsList:[],
			categorysList:[],
			value:'',
			fileList1:[],
			flag:1,
			// rules: {
			// 	'intro': {
			// 		type: 'string',
			// 		required: true,
			// 		message: '请填写内容',
			// 		trigger: ['blur', 'change']
			// 	},
			//}
		};
	},
	onLoad() {
		this.get()
		// uni.$on('id',(ros)=>{
		// 	console.log(ros.id);
		// })
	},
	methods: {
		submit() {
			// this.$refs.uForm.validate(valid => {
			// 	if (valid) {
			// 		console.log('验证通过');
			// 	} else {
			// 		console.log('验证失败');
			// 	}
			// });
		},
		async get(){
			const { data : res} =await uni.$http.get('/tags')
			if(res.code!==200) return uni.$showMsg()
			this.tagsList=res.data
			console.log(res);
			
			const { data : ros} =await uni.$http.get('/categorys')
			if(ros.code!==200) return uni.$showMsg()
			this.categorysList=ros.data
			console.log(ros);
		},
		async open(content,title,category,summary,tags1,contentHtml){
			const id = uni.getStorageSync('id');
			console.log(id);
			console.log(tags1);
			// if (!Array.isArray(tags)) {  
			//             console.error('tags is not an array!');  
			// 			return;
			//         } else{
			// 			console.error('tags is  an array!');  
			// 		}
			
			// contentHtml=content;
			let tags = tags1.map(function (item) {
			              return {id: item};
			            });
			const body={
				content:content,
				contentHtml:contentHtml
			}
			console.log(body);
			console.log(tags);
			console.log(category);
			const {data:my}=await uni.$http.post('/articles/publish',{	
				body,
				title,
				category,
				summary,
				tags,
				id
			})
			if(my.code!==200){
				return uni.$showMsg()
			}else{
				uni.redirectTo({  
				    url: `/homepky/ArticleContent/ArticleContent?id=${encodeURIComponent(JSON.stringify(my.data.id))}`,  
				    success: function(ri) {  
				        console.log(decodeURIComponent(ri)); // 解码传递的参数  
				    }  
				});
			}
			console.log(my);
		},
		// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log(result);
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise1(url){
				if(url){
					return url;
				}
			},
			async uploadFilePromise(url){
				console.log(url);
				const that=this;
				const data= wx.getFileSystemManager().readFile({
				  filePath: url,  
				  encoding: 'base64',  
				  success:async  (res) => {  
				    const base64Str = res.data  // 这就是你的 Base64 编码的字符串
					const {data:ris}=await uni.$http.post(`/upload`,{
						image:url,
						base64Str:base64Str,
						})
					if(ris.code!==200) return uni.$showMsg()
					if(that.flag==1){
						that.contentHtml=ris.data
						that.flag++
					}else{
						that.contentHtml=that.contentHtml+'@'+ris.data
					}
					console.log(that.contentHtml);
					console.log(ris);
				  },  
				  fail: (err) => {  
				    console.error(err)  
				  }  
				  });
				console.log(url);
			}
	}
}
</script>

<style lang="scss">
.cate-xz{
	margin-top: 15px;
}
</style>
